package com.monise.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.monise.calendario.JDateChooser;
import com.monise.dao.DAO.AberturaCaixaDAO;
import com.monise.dao.DAO.FechamentoCaixaDao;
import com.monise.model.AberturaCaixa;
import com.monise.model.FechamentoCaixa;
import com.monise.util.Util;

public class FrameAbertura extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtValorCaixa;
	private JTextArea txtObservacao;
	private JButton btnSalvar;
	private JButton btnLimpar;
	private final JDateChooser txtDataConsulta = new JDateChooser();
	private JTextField txtDataFechamento;
	private Util util = new Util();
	private JLabel lblValorFechamento;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameAbertura frame = new FrameAbertura();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameAbertura() {
		setClosable(true);
		setTitle("Abertura de Caixa");
		setBounds(100, 100, 536, 479);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));

		txtDataConsulta.getCalendarButton().setIcon(new ImageIcon(FrameAbertura.class.getResource("/com/monise/calendario/JDateChooserIcon.gif")));
		txtDataConsulta.setFont(new Font("Tahoma", Font.PLAIN, 11));
		txtDataConsulta.setBounds(275,9, 135, 28);
		panel.add(txtDataConsulta);
		
		JLabel label = new JLabel("Observa\u00E7\u00E3o:");
		
		JLabel label_1 = new JLabel("Valor caixa:");
		
		JLabel label_3 = new JLabel("Data:");
		
		txtValorCaixa = new JTextField();
		txtValorCaixa.setColumns(10);
		
		txtObservacao = new JTextArea();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 312, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(label_1)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtValorCaixa, 105, 105, 105)
							.addGap(41)
							.addComponent(label_3, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(74, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_3)
						.addComponent(label_1)
						.addComponent(txtValorCaixa, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(txtObservacao, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(37, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.setIcon(new ImageIcon(FrameAbertura.class.getResource("/com/monise/imagens/Save.png")));
		
		btnLimpar = new JButton("Limpar");
		btnLimpar.setIcon(new ImageIcon(FrameAbertura.class.getResource("/com/monise/imagens/Trash.png")));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Consultar fechamento de caixa", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(29, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 470, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 297, GroupLayout.PREFERRED_SIZE))
					.addGap(21))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addGap(24))
		);
		
		JLabel lblData = new JLabel("Data:");
		
		txtDataFechamento = new JFormattedTextField((util.setMascara("##/##/####")));
		txtDataFechamento.setColumns(10);
		
		JButton btnConsultar = new JButton("");
		btnConsultar.setIcon(new ImageIcon(FrameAbertura.class.getResource("/com/monise/imagens/pesquisa.png")));
		
		JLabel lblValorFechamentor = new JLabel("Valor fechamento (R$) :");
		
		lblValorFechamento = new JLabel("");
		lblValorFechamento.setForeground(Color.RED);
		lblValorFechamento.setFont(new Font("Tahoma", Font.PLAIN, 12));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblData)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(txtDataFechamento, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnConsultar))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(lblValorFechamentor)
							.addGap(18)
							.addComponent(lblValorFechamento)))
					.addContainerGap(246, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(13)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblData)
							.addComponent(txtDataFechamento, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnConsultar))
					.addGap(20)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblValorFechamentor)
						.addComponent(lblValorFechamento))
					.addContainerGap(30, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		
		getContentPane().setLayout(groupLayout);
		
		btnSalvar.addActionListener(new SalvarAbertura());
		btnLimpar.addActionListener(new LimparCampos());
		btnConsultar.addActionListener(new ConsultarFechamento());

	}
	
	private class SalvarAbertura implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			java.sql.Date data = new java.sql.Date(txtDataConsulta.getDate().getTime());  
			
			AberturaCaixa aberturaCaixa = new AberturaCaixa();
			aberturaCaixa.setDataAbertura(data);
			aberturaCaixa.setDescricao(txtObservacao.getText());
			String valorCaixa = txtValorCaixa.getText().replace(",", ".");
			aberturaCaixa.setValorAbertura(BigDecimal.valueOf(Double.parseDouble(valorCaixa)));
			
			try{
				AberturaCaixaDAO aberturaDao = new AberturaCaixaDAO();
				aberturaDao.save(aberturaCaixa);
				JOptionPane.showMessageDialog(btnSalvar, "Abertura de Caixa salvo com sucesso!");
				limpar();
			}catch(Exception ex){
				JOptionPane.showMessageDialog(btnSalvar, "Ocorreu um erro no momento de salvar!");
			}
		}
	}
	
	private class LimparCampos implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			limpar();
		}
	}
	
	private class ConsultarFechamento implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			getConsultaFechamento();
		}
	}
	
	private void limpar(){
		txtDataConsulta.setToolTipText("");
		txtObservacao.setText("");
		txtValorCaixa.setText("");
		txtDataConsulta.setDate(null);
	}
	
	private Date converterData(String data){
		DateFormat forma = new SimpleDateFormat("dd/MM/yyyy");    
	    java.util.Date dataNova = null;
		try {
			dataNova = new java.util.Date(forma.parse(data).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dataNova;
	}
	
	private void getConsultaFechamento(){
		FechamentoCaixaDao fechamentoDao = new FechamentoCaixaDao();
		FechamentoCaixa fechamento = fechamentoDao.getFechamento(converterData(txtDataFechamento.getText()));
		lblValorFechamento.setText(fechamento.getValorFechamento().toString());
	}
}
